package oneD.test;

import static java.lang.Math.*;

public class Vector {

	private double[] x;

	public Vector(int n) {
		x = new double[n];
	}

	public Vector(int n, double val) {
		x = new double[n];
		for (int i = 0; i < n; i++)
			x[i] = val;
	}

	public Vector(double... x) {
		this.x = x;
	}

	public int getN() {
		return x.length;
	}

	public double get(int idx) {
		return x[idx];
	}

	public void set(int idx, double val) {
		x[idx] = val;
	}

	public void set(int start, int len, double[] val) {
		for (int i = start; i < start + len; i++)
			x[i] = val[i - start];
	}

	public void fill(double val) {
		for (int i = 0; i < getN(); i++)
			x[i] = val;
	}

	public void print(String str) {
		System.out.print(str + ':');
		for (int i = 0; i < this.x.length; i++)
			System.out.print(this.x[i] + ", ");
	}

	public double scalarProduct(Vector v) {
		double d = 0;
		for (int i = 0; i < x.length; i++)
			d += x[i] * v.get(i);
		return d;
	}

	public double scalarProduct(double[] a, double[] b) {
		double d = 0.0;
		for (int i = 0; i < a.length; i++)
			d += a[i] * b[i];
		return d;
	}

	public Vector multiply(double alpha) {
		double[] res = new double[x.length];
		for (int i = 0; i < x.length; i++)
			res[i] = x[i] * alpha;
		return new Vector(res);
	}

	public void add(int idx, double val) {
		x[idx] += val;
	}// add to members in vector

	public void add(int start, int len, double[] val) {
		for (int i = start; i < start + len; i++)
			x[i] += val[i - start];
	}

	public void add(int start, int len, double alpha, double[] val) {
		for (int i = start; i < start + len; i++)
			x[i] += alpha * val[i - start];
	}

	public Vector add(Vector v) {
		double[] res = new double[x.length];
		for (int i = 0; i < x.length; i++)
			res[i] = x[i] + v.get(i);
		return new Vector(res);
	}

	public Vector add(double alpha, Vector v) {
		double[] res = new double[x.length];
		for (int i = 0; i < x.length; i++)
			res[i] = x[i] + alpha * v.get(i);
		return new Vector(res);
	}

	public double twoNorm() {
		return sqrt(this.scalarProduct(this));
	}

	public double twoNorm(double[] v) {
		return sqrt(this.scalarProduct(v, v));
	}

	public double oneNorm() {
		double d = 0;
		for (int i = 0; i < getN(); i++)
			d += abs(x[i]);
		return d;
	}

	public double maxNorm() {
		double d = abs(x[0]);
		for (int i = 0; i < getN(); i++)
			if (abs(x[i]) > d)
				d = abs(x[i]);
		return d;
	}

	public double[] toArray() {
		return x;
	}
}
